package com.mapler.ui;

import com.mapler.utility.HttpHelper;
import com.mapler.utility.IConstant;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;

/**
 *
 * @author none
 */
public class XAdImpUI extends JPanel {

    private String username;
    private String password;
    private String country;
    int count = 0;
    private JLabel message;

    public XAdImpUI() {
        initComponents();
    }

    public XAdImpUI(String username, String password, String country) {
        this.username = username;
        this.password = password;
        this.country = country;
        initComponents();
    }

    private void initComponents() {
        try {
            setLayout(null);
            setPreferredSize(new Dimension(600, 400));
            message = new javax.swing.JLabel();
            message.setBounds(20, 20, 120, 20);
            add(message);
            message.setText("Message");
            
            final JTextField jtFile = new JTextField(20);
            jtFile.setBounds(20, 40, 250, 25);
            add(jtFile);
            
            JButton jbImport = new JButton("Select File");            
            jbImport.setBounds(275, 40, 80, 25);
            add(jbImport);

            jbImport.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JFileChooser fc = new JFileChooser();
                    fc.addChoosableFileFilter(new OnlyExt());

                    int returnval = fc.showOpenDialog(null);
                    if (returnval == JFileChooser.APPROVE_OPTION) {
                        File file = fc.getSelectedFile();
                        jtFile.setText(file.getPath());
                    }
                }
            });
        } catch (Throwable ex) {
            ex.printStackTrace();
        }

        //Lay out the panel.
        /*SpringUtilities.makeCompactGrid(this,
         count+1, 1, //rows, cols
         6, 6, //initX, initY
         6, 6); //xPad, yPad
         */
    }

    private void update(String id, boolean checked) {
        try {
            int work = 0;
            if (checked) {
                work = 1;
            }
            StringBuilder request = new StringBuilder();
            request.append("<Request>");
            request.append("<username>").append(this.username).append("</username>");
            request.append("<password>").append(this.password).append("</password>");
            request.append("<country>").append(this.country).append("</country>");
            request.append("<id>").append(id).append("</id>");
            request.append("<working>").append(work).append("</working>");
            request.append("</Request>");
            String uri = "http://" + IConstant.HOSTNAME + "/index.php?r=account/updateWorking&request=" + request.toString();

            String response = HttpHelper.post(new URL(uri), "");

            Document document = DocumentHelper.parseText(response);
            String errorCode = document.valueOf("Response/errorcode");
            String msg = document.valueOf("Response/message");
            if (errorCode.equalsIgnoreCase("000")) {
                message.setBackground(Color.red);
            } else {
                this.message.setBackground(Color.black);
            }
            this.message.setText(msg);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
